<?php
/*
 * this contains forms and functions for mySite.php
 */

//this will display the function to edit the userinfo
function editContact($db, $error) {
    echo '<form name="registrer" method="post" action="mySite.php" >';
    echo '<h2>Brukerinformasjon</h2>';
    if ($error == 1) {
        echo '<div class="errorEdit">Unable to connect to database, please try again later</div>';
    } else if ($error == 2) {
        echo '<div class="successEdit">Your profile has successfully been updated</div>';
    }
    if (isset($_POST['edit'])) { // sets the form inputs to be editable
        $nonEditable = '';
        $disableDropdown = '';
    } else {
        $nonEditable = 'readonly="readonly"'; // sets the form inputs to be readonly 
        $disableDropdown = 'disabled="disabled"';
    }
    //displays the form with the user values
    $res = $db->query('SELECT * from users WHERE uid=' . $_SESSION['uid']);
    if ($row = $res->fetch()) {      
        echo '<label for="givenname">Fornavn:</label><input name="givenname" type="text" value="' . $row['givenname'] . '" required="required" ' . $nonEditable . ' ><br />';
        echo '<label for="surename">Etternavn:</label><input name="surename" type="text" value="' . $row['surename'] . '" required="required" ' . $nonEditable . ' ><br />';
        echo '<label for="address1">Adresse:</label><input name="address1" type="text" value="' . $row['address'] . '" required="required" ' . $nonEditable . ' ><br />';
        echo '<label for="country">Land:</label><select name="country"  value="options" ' . $disableDropdown . ' >';
        $res1 = $db->query('SELECT * from countries ORDER BY cid');
        while ($row1 = $res1->fetch()) { //display countries in dropdown
            if ($row1['cid'] == $row['country']) {
                echo "<option value='{$row1['country']}' selected='selected'>{$row1['country']}</option>\n";
            } else {
                echo "<option value='{$row1['country']}'>{$row1['country']}</option>\n";
            }
        }
        echo '</select><br />';
        echo '<label for = "zip">Postnummer:</label><input name = "zip" type = "text" value="' . $row['postnr'] . '" required = "required" ' . $nonEditable . ' ><br />';
        $state = null;
        if ($row['poststed'] == '') { // if norwegian user
            $res2 = $db->query('SELECT * from postnr');
            while ($row2 = $res2->fetch()) {            //get poststed
                if ($row2['postnr'] == $row['postnr']) { 
                    $state = $row2['poststed'];
                    break;
                }
            }
        } else {
            $state = $row['poststed']; // else poststed is poststed!
        }
        echo '<label for = "state">Poststed:</label><input name = "state" id="state" type = "text"  value="' . $state . '" ' . $nonEditable . ' ><br />';

        if (isset($_POST['edit'])) { //if in edit mode
            echo '<input name="save" type="hidden" value="save">';
            echo '<input name = "mySave" type = "submit" value = "Lagre" ><input name = "reset" type = "reset" value = "Reset"><a href="mySite.php" ><input name = "cancel" type = "button" value = "Avbryt"></a>';
        } else { //else in readonly mode
            echo '<input name="edit" type="hidden" value="edit">';
            echo '<input name = "myEdit" type = "submit" value = "Endre" >';
        }
    } else {
        echo "user not found";
    }

    echo '</form>';
}
// this will display a form to edit the password
function editPassword() {
    echo '<form name="password" method="post" action="mySite.php" >';
    echo '<h2>Endre passord</h2>';
    if($_REQUEST['pass'] == 'error'){ //check if the password is correct
        echo '<div class="validationEdit">Your old password is incorrect!</div>';
    }
    //inputs to change the password
    echo '<label for = "password">Gammelt password:</label><input name = "oldPassword"  type = "password" required = "required" maxlength = "15" ><br />';
    echo '<label for = "password">Nytt Passord:</label><input name = "password" type = "password" required = "required" placeholder="Between 6-15 characters" maxlength = "15"><br />';
    echo '<label for = "check">Bekreft:</label><input type = "password" name = "check" data-equals = "password" required = "required" /><br />';
    echo '<input name = "mySave" type = "submit" value = "Lagre" >';
    echo '<input name = "reset" type = "reset" value = "Reset"><a href="mySite.php" ><input name = "cancel" type = "button" value = "Avbryt"></a>';    
    echo '</form>';
}
//this displays a form to edit the username(email)
function editUsername($db) {
    echo '<form name="password" method="post" action="mySite.php" >';
    echo '<h2>Endre brukernavn</h2>';
    $res = $db->query('SELECT email from users WHERE uid=' . $_SESSION['uid']);
    $row = $res->fetch(); // get the email if set
    echo '<label for = "email">E-Post:</label><input name = "emailEdit" type = "email" value="' . $row['email'] . '" required = "required"><br />';
    echo '<input name = "mySave" type = "submit" value = "Lagre" >';
    echo '<input name = "reset" type = "reset" value = "Reset"><a href="mySite.php" ><input name = "cancel" type = "button" value = "Avbryt"></a>';        
    echo '</form>';    
}

?>

